package com.cmcc.passenger.ui.onorder

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cmcc.passenger.data.AppRequest
import com.cmcc.passenger.data.PassDataRepository
import com.cmcc.passenger.data.entity.response.Gratuity
import com.cmcc.passenger.data.entity.response.NearbyCar
import com.cmcc.passenger.data.entity.response.OrderRecord
import com.cmcc.passenger.data.entity.response.OrderTemplate
import com.cmcc.shared.Event
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class OnOrderViewModel : ViewModel() {

    private val _cancelSelector = MutableLiveData<Event<Unit>>()
    val orderTemplate = MutableLiveData<OrderTemplate>()

    val nearbyCar = MutableLiveData<List<NearbyCar>>()

    val updatedSuccess = MutableLiveData<Boolean>()
    val cancelCall = MutableLiveData<Int>()

    val processOrder = MutableLiveData<OrderRecord>()

    fun getTipList(): MutableLiveData<List<Gratuity>> {
        return PassDataRepository.getInstance().getTips()
    }

    fun cancelSelector(): LiveData<Event<Unit>> {
        return _cancelSelector
    }

    fun onCancelClick() {
        _cancelSelector.value = Event(Unit)
    }

    /**
     * 获取订单模板
     */
    fun getOrderTemplate(id: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getOrderTemplate(id)
                if (response.isSuccessful) {
                    val data = response.body()
                    orderTemplate.value = data?.data
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 获取附近司机
     */
    fun getNearbyDriver(lat: Double?, lng: Double?) {
        if (lat != null && lng != null)
            viewModelScope.launch(Dispatchers.Main) {
                try {
                    val response = AppRequest.getInstance().nearbyCar(lat, lng)
                    if (response.isSuccessful) {
                        val data = response.body()
                        nearbyCar.value = data?.data
                    }
                } catch (e: Exception) {
                    L.d("e :" + e.message + "," + e.cause)
                }
            }
    }

    /**
     * 更新小费
     */
    fun updateBounty(orderId: String, amount: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().updateBounty(orderId, amount)
                if (response.isSuccessful) {
                    val data = response.body()
                    updatedSuccess.value = data?.data


                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 放弃叫车
     * @param id order id
     * @param remark 取消原因
     */
    fun cancelCall(id : String,remark : String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().cancelCall(id, remark)
                if (response.isSuccessful) {
                    val data = response.body()
                    cancelCall.value = data?.code
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 查询正在进行的订单
     */
    fun queryOrderProgress() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().queryOrderInProgress()
                if (response.isSuccessful) {
                    val post = response.body()
                    processOrder.value = post?.data
                } else {
                    L.d("server error")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }
}